﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ctrlwct
{
    public partial class UserEdit : Form
    {
        public string user { get; set; }
        public string Nombre { get; set; }
        public string useredit { get; set; }
        public string SA { get; set; }
        public string Role { get; set; }
        public string actuser { get; set; }
        public string str;
        public UserEdit()
        {
            InitializeComponent();
            Tiempo.Enabled = true;
            Tiempo.Interval = 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                if (useredit == "N")
                {
                    
                    switch (SA)
                    {
                        case "Y":
                        comboBox1.Text = "Yes";
                        break;
                        case "N":
                        comboBox1.Text = "No";
                        break;
                    }
                    switch (Role)
                    {
                        case "S":
                            comboBox2.Text = "Super Admin";
                            break;
                        case "U":
                            comboBox2.Text = "Normal User";
                            break;
                    }
                    if(actuser != user)
                    {
                        switch (comboBox1.Text)
                        {
                            case "Yes":
                                SA = "Y";
                                break;
                            case "No":
                                SA = "N";
                                break;
                        }
                        switch (comboBox2.Text)
                        {
                            case "Super Admin":
                                Role = "S";
                                break;
                            case "Normal User":
                                Role = "U";
                                break;

                        }
                        try
                        {
                            using (MySqlConnection conexion = GuiLogin.Conexion.ObtenConexion())
                            {
                                conexion.Open();

                                using (MySqlCommand insert = new MySqlCommand("INSERT INTO Users (User,Nombre,SA,Role) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + SA + "','" + Role + "')", conexion))
                                    insert.ExecuteNonQuery();
                                conexion.Close();
                                Passwdchng frm = new Passwdchng();
                                frm.user = textBox1.Text;
                                if (frm.ShowDialog() == DialogResult.OK)
                                {
                                    this.DialogResult = DialogResult.OK;

                                    this.Close();
                                }
                            }

                                
                            }
                        catch {
                            MessageBox.Show("Error, El usuario ya existe");
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        
                    
 
                    }
                    else
                    {
                        MessageBox.Show("Error, No se permite editar el usuario activo", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }

                }
                if (useredit == "Y")
                {
                    if (textBox1.Text != "")
                    {
                        switch (SA)
                        {
                            case "Y":
                                comboBox1.Text = "Yes";
                                break;
                            case "N":
                                comboBox1.Text = "No";
                                break;
                        }
                        switch (Role)
                        {
                            case "S":
                                comboBox2.Text = "Super Admin";
                                break;
                            case "U":
                                comboBox2.Text = "Normal User";
                                break;
                        }
                        if (actuser != user)
                        {
                            switch (comboBox1.Text)
                            {
                                case "Yes":
                                    SA = "Y";
                                    break;
                                case "No":
                                    SA = "N";
                                    break;
                            }
                            switch (comboBox2.Text)
                            {
                                case "Super Admin":
                                    Role = "S";
                                    break;
                                case "Normal User":
                                    Role = "U";
                                    break;

                            }
                            using (MySqlConnection conexion = GuiLogin.Conexion.ObtenConexion())
                            {
                                conexion.Open();
                                using (MySqlCommand comando = new MySqlCommand("UPDATE Users SET Nombre='" + textBox2.Text + "',SA='" + SA + "',Role='" + Role + "' WHERE User='" + textBox1.Text + "'", conexion))
                                    comando.ExecuteNonQuery();
                                conexion.Close();
                                this.DialogResult = DialogResult.OK;
                                this.Close();
                               
                            }
                        }
                    }
                }
             
            }
            else
            {
                MessageBox.Show("Error, debe ingresar un usuario", "Error",MessageBoxButtons.OK ,MessageBoxIcon.Stop);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Tiempo.Stop();
            textBox1.Text = user;
            textBox2.Text = Nombre;
            if (useredit == "Y")
            {
                textBox1.ReadOnly = true;
            }
            switch (SA)
            {
                case "Y":
                    comboBox1.Text = "Yes";
                    break;
                case "N":
                    comboBox1.Text = "No";
                    break;
            }
            switch (Role)
            {
                case "S":
                    comboBox2.Text = "Super Admin";
                    break;
                case "U":
                    comboBox2.Text = "Normal User";
                    break;
            }
        }
    }
}
